﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SpaceNeedle.x6473.WindowsPhone.Resources;
using System.Windows.Media;
using Microsoft.Phone.Maps.Toolkit;
using Microsoft.Phone.Maps.Controls;
using System.Device.Location;

namespace x6473WindowsPhone
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.pivot.SelectionChanged += SelectionChanged;
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();

            //-23.4955, -46.8476

            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.map.Center = new System.Device.Location.GeoCoordinate(-23.4955, -46.8476);
            this.map.ZoomLevel = 14;

            MapLayer layer0 = new MapLayer();

            Pushpin pushpin0 = new Pushpin();
            pushpin0.Content = "01";
            //Pushpin pushpin0 = (Pushpin)this.FindName("pushpin0");
            //Pushpin pushpin0 = MapExtensions.GetChildren(myMap).OfType<Pushpin>().First(p => p.Name == "pushpin0");
            //if (pushpin0 == null) { pushpin0 = new Pushpin(); }
            //pushpin0.GeoCoordinate = new GeoCoordinate(-23.4955, -46.8476);
            MapOverlay overlay0 = new MapOverlay();
            overlay0.Content = pushpin0;
            overlay0.GeoCoordinate = new GeoCoordinate(-23.4955, -46.8476);
            layer0.Add(overlay0);

            MapLayer layer1 = new MapLayer();
            Pushpin pushpin1 = new Pushpin();
            pushpin1.Content = "02";
            //pushpin1.GeoCoordinate = new GeoCoordinate(-23.4945, -46.8476);
            MapOverlay overlay1 = new MapOverlay();
            overlay1.Content = pushpin1;
            overlay1.GeoCoordinate = new GeoCoordinate(-23.4875, -46.8426);
            layer0.Add(overlay1);

            Pushpin pushpin2 = new Pushpin();
            pushpin2.Content = "03";
            MapOverlay overlay2 = new MapOverlay();
            overlay2.Content = pushpin2;
            overlay2.GeoCoordinate = new GeoCoordinate(-23.4975, -46.835);
            layer0.Add(overlay2);

            Pushpin pushpin3 = new Pushpin();
            pushpin3.Content = "04";
           
            MapOverlay overlay3 = new MapOverlay();
            overlay3.Content = pushpin3;
            overlay3.GeoCoordinate = new GeoCoordinate(-23.4975, -46.8535);
            layer0.Add(overlay3);

            //// Add the layer with the pins in to the map
            map.Layers.Add(layer0);
           // map.Layers.Add(layer1);
        }

        void SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.map.Visibility = this.pivot.SelectedIndex == 0 ? Visibility.Collapsed : Visibility.Visible;
            
            if(this.map.Visibility == System.Windows.Visibility.Visible)
            {
                this.pivot.Foreground = new SolidColorBrush(Colors.Black);
               
            }else
            {
                this.pivot.Foreground = new SolidColorBrush(Colors.White);

            }

            this.title.Foreground = this.pivot.Foreground;
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}